// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
import QtQuick 1.1
import com.nokia.meego 1.0
import "qrc:/storage.js" as Storage

Page {
    id: xbmcUI
    property string iPaddress: "127.0.0.1"
    property string xbmcport: "9777"
    tools: xbmcToolLayout
    anchors.fill: parent
    Component.onCompleted: {
        Storage.initialize();
        settingTimer.running = true;
        //pingTimer.running = true;
    }
    //onIPaddressChanged: {console.log("IP changed to "+iPaddress)}
    //onXbmcportChanged: {console.log("port changed to "+xbmcport)}
    // set some settings
    Timer { // using a timer instead of Component.onCompleted as XbmcQtClient might not have been exposed yet..?
        id: settingTimer
        interval: 100; running: false; repeat: false;
        onTriggered: {
            iPaddress = Storage.getSetting("IP");
            xbmcport = Storage.getSetting("port");
            if (iPaddress === "000000000000" || xbmcport === "000000000000"){ // "000000000000" is a default value given for an unexisting entry in storage.js
                Storage.setSetting("IP","127.0.0.1"); Storage.setSetting("port","9777");
                iPaddress = "127.0.0.1"; xbmcport = "9777";
            }
            XbmcQtClient.setServerSettings(iPaddress, parseInt(xbmcport,10));
            XbmcQtClient.sendHeloToXBMC();
        }
    }
    // timer for pinging the server (this might not be required)
    /*
    Timer{
        id: pingTimer
        interval: 59000; running: false; repeat: true;
        //running: windowState.active
        onTriggered: XbmcQtClient.pingXBMC();
    }
    */

    //
    // The actual page
    //

    // IP address and Port settings
    Row{
        id: addressRow
        anchors.top: parent.top
        anchors.left: parent.left
        anchors.right: parent.right
        Column{
            Label{text: "XBMC IP"}
            TextField{
                id: ipInputField
                inputMethodHints: Qt.ImhDigitsOnly
                validator:RegExpValidator{
                    regExp:/^(([01]?[0-9]?[0-9]|2([0-4][0-9]|5[0-5]))\.){3}([01]?[0-9]?[0-9]|2([0-4][0-9]|5[0-5]))$/
                }
                width: xbmcUI.width/2
                text: iPaddress
                onAccepted: {iPaddress = ipInputField.text; Storage.setSetting("IP",iPaddress); XbmcQtClient.setServerSettings(iPaddress, parseInt(xbmcport,10)); XbmcQtClient.sendHeloToXBMC();}
            }
        }
        Column{
            Label{text: "XBMC port"}
            TextField{
                id: portInputField
                inputMethodHints: Qt.ImhDigitsOnly
                inputMask: "00000"
                width: xbmcUI.width/4
                text: xbmcport
                onAccepted: {xbmcport = portInputField.text; Storage.setSetting("port",xbmcport); XbmcQtClient.setServerSettings(iPaddress, parseInt(xbmcport,10)); XbmcQtClient.sendHeloToXBMC();}
            }
        }
    }

    // Remote control buttons
    Column{
        anchors.top: addressRow.bottom;
        anchors.topMargin: 70
        anchors.horizontalCenter: parent.horizontalCenter

        id: xbmcColumn
        spacing: 30
        Row{
            anchors.horizontalCenter: parent.horizontalCenter
            spacing: 40
            /////////// HOME BUTTON
            //ToolIcon { iconId: "toolbar-home"; onClicked: {XbmcQtClient.sendKeyToXBMC("backspace");}  }
            /////////// BACK BUTTON
            ToolButton {text:"Back"; anchors.verticalCenter: parent.verticalCenter; onClicked: {XbmcQtClient.sendKeyToXBMC("backspace");} }
            /////////// CONTEXT MENU
            ToolButton {text:"Context"; anchors.verticalCenter: parent.verticalCenter; onClicked: {XbmcQtClient.sendKeyToXBMC("C");} }
        }
        ToolButton {text:"Fullscreen"; anchors.horizontalCenter: parent.horizontalCenter; onClicked: {XbmcQtClient.sendKeyToXBMC("tab");} }
        //ToolIcon { iconId: "toolbar-mediacontrol-play" }
        /////////// UP BUTTON
        ToolIcon { iconId: "toolbar-up"; anchors.horizontalCenter: parent.horizontalCenter; onClicked: {XbmcQtClient.sendKeyToXBMC("up");}  }
        Row{
            spacing: 20
            anchors.horizontalCenter: parent.horizontalCenter
            /////////// < BUTTON
            ToolIcon { iconId: "toolbar-previous"; onClicked: {XbmcQtClient.sendKeyToXBMC("left");}  }
            /////////// ENTER BUTTON
            ToolButton {text:"Select / OSD"; anchors.verticalCenter: parent.verticalCenter; onClicked: {XbmcQtClient.sendKeyToXBMC("return");}}
            /////////// > BUTTON
            ToolIcon { iconId: "toolbar-next"; onClicked: {XbmcQtClient.sendKeyToXBMC("right");}  }
        }
        /////////// DOWN BUTTON
        ToolIcon { iconId: "toolbar-down"; anchors.horizontalCenter: parent.horizontalCenter; onClicked: {XbmcQtClient.sendKeyToXBMC("down");}  }
        Row{
            anchors.horizontalCenter: parent.horizontalCenter
            spacing: 40
            /////////// Vol -
            ToolButton {text:"Vol-"; anchors.verticalCenter: parent.verticalCenter; onClicked: {XbmcQtClient.sendKeyToXBMC("minus");} }
            /////////// Vol +
            ToolButton {text:"Vol+"; anchors.verticalCenter: parent.verticalCenter; onClicked: {XbmcQtClient.sendKeyToXBMC("plus");} }
        }
    }
    // BOTTOM TOOLBAR
    ToolBarLayout {
        id: xbmcToolLayout
        visible: true
        anchors { left: parent.left; right: parent.right; bottom: parent.bottom }
        ToolIcon { iconId: "toolbar-back"; onClicked: pageStack.pop();}
        ToolIcon { iconId: "toolbar-mediacontrol-backwards"; onClicked: {XbmcQtClient.sendKeyToXBMC("r");}  }
        ToolIcon { iconId: "toolbar-mediacontrol-stop"; onClicked: {XbmcQtClient.sendKeyToXBMC("X");}  }
        ToolIcon { iconId: "toolbar-mediacontrol-pause"; onClicked: {XbmcQtClient.sendKeyToXBMC("play_pause");}  }
        ToolIcon { iconId: "toolbar-mediacontrol-forward"; onClicked: {XbmcQtClient.sendKeyToXBMC("f");}  }
        ToolIcon {
            platformIconId: "toolbar-view-menu"
            anchors.right: (parent === undefined) ? undefined : parent.right
            onClicked: (myXbmcMenu.status == DialogStatus.Closed) ? myXbmcMenu.open() : myXbmcMenu.close()
        }
    }
    //
    //
    // BOTTOM MENU
    //
    //
    Menu {
        id: myXbmcMenu
        visualParent: pageStack
        //platformStyle: MenuStyle
        MenuLayout {
            MenuItem {
                text: qsTr("Reconnect to XBMC");
                onClicked: XbmcQtClient.sendHeloToXBMC()
            }
            MenuItem {
                text: qsTr("About");
                onClicked:{pageStack.push(Qt.resolvedUrl("WolHelpPage.qml"))}
            }
        }
    } // end Menu
}
